package com.example.demo.utils;

/**
 * TODO
 *
 * @author mazh47622
 * @date2023/12/8
 * @modify -----------------------------------------------------------------------------------------------------
 * |修改单号|修改人员|修改日期|评审人员|修改说明
 * -----------------------------------------------------------------------------------------------------
 * |      |       |      |       |
 * -----------------------------------------------------------------------------------------------------
 */

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.Random;

/**
 * 发送短信工具类
 */
@Repository
public class MessageUtil {

    @Value("${phone.code.api.url}")
    private static String PHONE_CODE_API_URL;

    @Value("${phone.code.api.id}")
    private static String PHONE_CODE_API_ID;

    @Value("${phone.code.api.signature}")
    private static String PHONE_CODE_API_SIGNATURE;


    public static String sendMessage(String phone, String code) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // api 地址
        String url = "https://api-v4.mysubmail.com/sms/xsend";
        // 需要使用 POST 请求
        HttpPost httpPost = new HttpPost(url);
        JSONObject jsonParam = new JSONObject();
        // 创建的 APPID
        jsonParam.put("appid", "58732");
        // 创建的 APPID 的 APPKEY
        jsonParam.put("signature", "f7c84c072492880ffeb6441ff50d0c11");
        // 你的模板编号，见 https://www.mysubmail.com/chs/documents/developer/MmSw12
        jsonParam.put("project", "M3Qh22");

        // 创建的 APPID 的 APPKEY
//        jsonParam.put("signature", "f7c84c072492880ffeb6441ff50d0c11");

        // 你的模板编号，见 https://www.mysubmail.com/chs/documents/developer/MmSw12
        jsonParam.put("project", "KL535");
        // 接收人手机号
        jsonParam.put("to", phone);
        JSONObject vars = new JSONObject();
        // 模板变量
        vars.put("code", code);
        jsonParam.put("vars", vars.toJSONString());
        StringEntity entity = new StringEntity(jsonParam.toJSONString(), "UTF-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        // 响应体
        HttpResponse resp = httpclient.execute(httpPost);
        String respContent = null;
        if (resp.getStatusLine().getStatusCode() == 200) {
            HttpEntity he = resp.getEntity();
            respContent = EntityUtils.toString(he, "UTF-8");
        }
        return respContent;


    }

    public static String getSixPhoneCode(){
        String sources = "0123456789"; // 加上一些字母，就可以生成pc站的验证码了
        Random rand = new Random();
        StringBuffer flag = new StringBuffer();
        for (int j = 0; j < 6; j++)
        {
            flag.append(sources.charAt(rand.nextInt(9)) + "");
        }

        return flag.toString();
    }
}
